package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        //插入套餐表数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //获取setmealId
        Long setmealId = setmeal.getId();
        //获取setmeal_dishes
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && setmealDishes.size()>0){
            //插入setmeal_dish表数据
           setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
           setmealDishMapper.insert(setmealDishes);
        }
    }
    /**分页查询
     * @param setmealPageQueryDTO
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 套餐起售停售功能
     * @param status，id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }
    /**
     * 套餐起售停售功能批量删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //套餐是否为停售状态
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.selectById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            //删除套餐表
            setmealMapper.deleteById(id);
            //根据setmealId删除setmeal_dish中关联的数据的
            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    @Override
    public SetmealVO selectByIdWithDishes(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //根据setmealId，在setmeal_dish表中查找dishes数据
        List<SetmealDish> setmealDishes=setmealDishMapper.selectBysetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    /**
     * 更新套餐信息
     * @param setmealVO
     */
    @Transactional
    @Override
    public void update(SetmealVO setmealVO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        //修改setmeal表
        setmealMapper.update(setmeal);
        //删除所有的setmealDishes
        setmealDishMapper.deleteBySetmealId(setmealVO.getId());
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealVO.getId()));
        //重新插入删除所有的setmealDishes
        if(setmealDishes!=null && setmealDishes.size()>0){
            setmealDishMapper.insert(setmealDishes);
        }
    }

    @Override
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        List<Setmeal> list=setmealMapper.selectByCategoryId(categoryId);
        return list;
    }
    /**
     * 根据id查询套餐
     * @param id
     */
    /*@Override
    public SetmealDTO selectByIdWithDishes(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealDTO setmealDTO = new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //根据setmealId，在setmeal_dish表中查找dishes数据
        List<SetmealDish> setmealDishes=setmealDishMapper.selectBysetmealId(id);
        setmealDTO.setSetmealDishes(setmealDishes);
        return setmealDTO;
    }*/

}
